package br.com.dev.tcc.database.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.com.dev.tcc.bean.Apolice;
import br.com.dev.tcc.bean.sinistro.Sinistro;
import br.com.dev.tcc.database.DatabaseHelper;

public class SinistroDAO {

	private final DatabaseHelper databaseHelper;

	public SinistroDAO(DatabaseHelper databaseHelper) {
		this.databaseHelper = databaseHelper;
	}

	public boolean salva(Context context, Sinistro sinistro) {

		ContentValues values = new ContentValues();
		values.put("id", sinistro.getId());
		values.put("id_apolice", sinistro.getApolice().getId());
		values.put("data", sinistro.getData());
		values.put("status", sinistro.getStatus());
		values.put("status_mensagem", sinistro.getStatusMensagem());
		if (sinistro.getTipo() != null)
			values.put("tipo",
					context.getString(sinistro.getTipo().getStringId()));
		else
			values.put("tipo", sinistro.getTipoDescricao());
		values.put("local", sinistro.getLocal());
		values.put("descricao", sinistro.getDescritivo());

		SQLiteDatabase database = databaseHelper.getWritableDatabase();

		long resultado = database.insertOrThrow("sinistro", null, values);

		database.close();
		
		return resultado > -1;
	}

	public List<Sinistro> lista() {

		List<Sinistro> lista = new ArrayList<Sinistro>();

		SQLiteDatabase db = databaseHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"SELECT S.id, S.tipo, S.data, S.local, S.status, S.descricao, S.status_mensagem, V.modelo, V.placa, A.hash "
								+ "FROM sinistro AS S JOIN apolice AS A ON S.id_apolice=A.id JOIN veiculo AS v ON A.id_veiculo=v.id",
						null);

		for (int i = 0; i < cursor.getCount(); i++) {

			cursor.moveToNext();

			Sinistro sinistro = new Sinistro();
			sinistro.setId(cursor.getInt(cursor.getColumnIndex("id")));
			sinistro.setTipoDescricao(cursor.getString(cursor
					.getColumnIndex("tipo")));
			sinistro.setData(cursor.getString(cursor.getColumnIndex("data")));
			sinistro.setLocal(cursor.getString(cursor.getColumnIndex("local")));
			sinistro.setStatus(cursor.getString(cursor.getColumnIndex("status")));
			sinistro.setDescritivo(cursor.getString(cursor
					.getColumnIndex("descricao")));
			sinistro.setStatusMensagem(cursor.getString(cursor
					.getColumnIndex("status_mensagem")));
			sinistro.setVeiculoModelo(cursor.getString(cursor
					.getColumnIndex("modelo")));
			sinistro.setVeiculoPlaca(cursor.getString(cursor
					.getColumnIndex("placa")));

			Apolice apolice = new Apolice();
			apolice.setHash(cursor.getString(cursor.getColumnIndex("hash")));
			sinistro.setApolice(apolice);

			lista.add(sinistro);
		}

		cursor.close();
		db.close();

		return lista;
	}

	public boolean atualizaStatus(Sinistro sinistro) {

		ContentValues values = new ContentValues();
		values.put("status", sinistro.getStatus());
		values.put("status_mensagem", sinistro.getStatusMensagem());

		SQLiteDatabase database = databaseHelper.getWritableDatabase();

		long resultado = database.update("sinistro", values,
				"id=" + Integer.toString(sinistro.getId()), null);

		return resultado > -1;
	}

	public long removerTodosPorApolice(int idApolice) {

		SQLiteDatabase database = databaseHelper.getWritableDatabase();

		long resultado = database.delete("sinistro", "id_apolice=?",
				new String[] { Integer.toString(idApolice) });

		database.close();
		
		return resultado;
	}

	public boolean remove(int id) {

		SQLiteDatabase database = databaseHelper.getWritableDatabase();

		long resultado = database.delete("sinistro", "id=?",
				new String[] { Integer.toString(id) });

		database.close();
		
		return resultado > -1;
	}
}
